Replace Text UserScript
https://gyazo.com/9f307265bee722a5afdaca164ef24bed
code:js
import '/api/code/progfay-pub/Replace_Text_UserScript/script.js'
正規表現も使用できる
一度に複数個の置換を順次実行することも可能
書き方: (置換前の文字列/正規表現) => (置換後の文字列)
置換コマンドを並べた後には、空行を入れること
code:format
(before) => (after)
(before) => (after)
:
(before) => (after)
(text)
(text)
:
(text)
code:sample1
/a|\s/g=>b
ab a b a aba b a ba bb
code:sample2
/\s/g => fuga
hoge hoge hoge
/icons/hr.icon
code:script.js
const replaceText = text => {
const splitPos = text.indexOf('\n\n')
const commands = text.substr(0, splitPos).split('\n').map(command => {
return { before, after }
})
let lines = text.substr(splitPos + 2).split('\n')
for (const { before, after } of commands) {
const match = before.match(new RegExp('^/(.*?)/(gimy*)$')) const reg = match ? new RegExp(match1, match2) : before lines = lines.map(line => line.replace(reg, after.replace(/\\n/g, '\n')))
}
return lines.join('\n')
}
scrapbox.PopupMenu.addButton({
title: 'replace',
onClick: replaceText
})